package cn.com.pc.content.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @Author hjlu
 * @create 2021/4/30 上午9:09
 */
@Configuration
public class ThreadPoolConfig {

    // 核心线程数量
    private static int corePoolSize = Runtime.getRuntime().availableProcessors();

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(corePoolSize * 2 + 1);
        executor.setQueueCapacity(1000);
        executor.setKeepAliveSeconds(180);
        executor.setThreadNamePrefix("pcp-thread-pool");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        return executor;
    }
}
